package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;

/**
 * 作者:dugaofeng on 2024/10/18 16:23
 */
public class ModeSettingItemView extends LinearLayout {

    private TextView nameTv;
    private ImageView arrowIv;

    private TextView valueTv;

    private boolean mutiItem;
    private ModeSettingBean.ModeSettingItemTag itemTag;
    private ModeSettingBean.ModeSettingItemBean settingItemBean;

    private OnModeSettingItemViewListner onModeSettingItemViewListner;

    public ModeSettingItemView(@NonNull Context context) {
        this(context, null);
    }

    public ModeSettingItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModeSettingItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void setOnModeSettingItemViewListner(OnModeSettingItemViewListner onModeSettingItemViewListner) {
        this.onModeSettingItemViewListner = onModeSettingItemViewListner;
    }

    public void setMutiItem(boolean mutiItem) {
        this.mutiItem = mutiItem;
        arrowIv.setVisibility(mutiItem ? VISIBLE : INVISIBLE);
    }

    public void setItemTag(ModeSettingBean.ModeSettingItemTag itemTag) {
        this.itemTag = itemTag;
    }

    public ModeSettingBean.ModeSettingItemTag getItemTag() {
        return itemTag;
    }

    private void initView() {
        inflate(getContext(), R.layout.view_mode_train_custom_item, this);

        nameTv = findViewById(R.id.tv_name);
        arrowIv = findViewById(R.id.iv_arrow);
        valueTv = findViewById(R.id.tv_value);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onModeSettingItemViewListner != null) {
                    onModeSettingItemViewListner.onModeSettingItemViewValueClick(ModeSettingItemView.this);
                }
            }
        });
    }

    public ModeSettingBean.ModeSettingItemBean getSettingItemBean() {
        return settingItemBean;
    }

    public void setSettingItemBean(ModeSettingBean.ModeSettingItemBean settingItemBean) {
        this.settingItemBean = settingItemBean;

        if (settingItemBean == null) {
            return;
        }
        nameTv.setText(settingItemBean.getName());
        String text = null;
        if (settingItemBean.isConstant()) {
            text = settingItemBean.getConstantValue();
        }
        else if (settingItemBean.isChooseRange()) {
            text = settingItemBean.getCurrentMinValue() + " ~ " + settingItemBean.getCurrentMaxValue();
        } else {
            text = "" + settingItemBean.getCurrentValue();
        }
        valueTv.setText(text);
    }

    interface OnModeSettingItemViewListner {
        void onModeSettingItemViewValueClick(ModeSettingItemView itemView);
    }
}
